<?php
	require('UploadHandler.php');
	
	class UploadHandlerJoy extends UploadHandler
	{
		function __construct($options = null, $initialize = true) {
			$this->options = array(
				'script_url' => $this->get_full_url().'/',
				'upload_dir' => $_SERVER['DOCUMENT_ROOT'].'/orders/',
				'upload_url' => $_SERVER['DOCUMENT_ROOT'].'/orders/',
				'user_dirs' => false,
				'mkdir_mode' => 0755,
				'param_name' => 'files',
				// Set the following option to 'POST', if your server does not support
				// DELETE requests. This is a parameter sent to the client:
				'delete_type' => 'DELETE',
				'access_control_allow_origin' => '*',
				'access_control_allow_credentials' => false,
				'access_control_allow_methods' => array(
					/*'OPTIONS',
					'HEAD',
					'GET',*/
					'POST'/*,
					'PUT',
					'PATCH',
					'DELETE'*/
				),
				'access_control_allow_headers' => array(
					'Content-Type',
					'Content-Range',
					'Content-Disposition'
				),
				// Enable to provide file downloads via GET requests to the PHP script:
				'download_via_php' => false,
				// Defines which files can be displayed inline when downloaded:
				//'inline_file_types' => '/\.(gif|jpe?g|png)$/i',
				// Defines which files (based on their names) are accepted for upload:
				'accept_file_types' => '/\.(zip|rar)$/i',
				// The php.ini settings upload_max_filesize and post_max_size
				// take precedence over the following max_file_size setting:
				'max_file_size' => null,
				'min_file_size' => 1,
				// The maximum number of files for the upload directory:
				'max_number_of_files' => null,
				// Image resolution restrictions:
				'max_width' => null,
				'max_height' => null,
				'min_width' => 1,
				'min_height' => 1,
				// Set the following option to false to enable resumable uploads:
				'discard_aborted_uploads' => true,
				// Set to true to rotate images based on EXIF meta data, if available:
				'orient_image' => false,
				'image_versions' => array(
					// Uncomment the following version to restrict the size of
					// uploaded images:
					/*
					'' => array(
						'max_width' => 1920,
						'max_height' => 1200,
						'jpeg_quality' => 95
					),
					*/
					// Uncomment the following to create medium sized images:
					/*
					'medium' => array(
						'max_width' => 800,
						'max_height' => 600,
						'jpeg_quality' => 80
					),
					*/
					'thumbnail' => array(
						'max_width' => 80,
						'max_height' => 80
					)
				)
			);
			if ($options) {
				$this->options = array_merge($this->options, $options);
			}
			if ($initialize) {
				$this->initialize();
			}
		}
		
		public function post($print_response = true) {
			$upload = isset($_FILES[$this->options['param_name']]) ?
				$_FILES[$this->options['param_name']] : null;
			// Parse the Content-Disposition header, if available:
			$file_name = isset($_SERVER['HTTP_CONTENT_DISPOSITION']) ?
				rawurldecode(preg_replace(
					'/(^[^"]+")|("$)/',
					'',
					$_SERVER['HTTP_CONTENT_DISPOSITION']
				)) : null;
			// Parse the Content-Range header, which has the following form:
			// Content-Range: bytes 0-524287/2000000
			$content_range = isset($_SERVER['HTTP_CONTENT_RANGE']) ?
				preg_split('/[^0-9]+/', $_SERVER['HTTP_CONTENT_RANGE']) : null;
			$size =  $content_range ? $content_range[3] : null;
			$files = array();
			if ($upload && is_array($upload['tmp_name'])) {
				// param_name is an array identifier like "files[]",
				// $_FILES is a multi-dimensional array:
				foreach ($upload['tmp_name'] as $index => $value) {
					$files[] = $this->handle_file_upload(
						$upload['tmp_name'][$index],
						$file_name ? $file_name : $upload['name'][$index],
						$size ? $size : $upload['size'][$index],
						$upload['type'][$index],
						$upload['error'][$index],
						$index,
						$content_range
					);
				}
			} else {
				// param_name is a single object identifier like "file",
				// $_FILES is a one-dimensional array:
				$files[] = $this->handle_file_upload(
					isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
					$file_name ? $file_name : (isset($upload['name']) ?
							$upload['name'] : null),
					$size ? $size : (isset($upload['size']) ?
							$upload['size'] : $_SERVER['CONTENT_LENGTH']),
					isset($upload['type']) ?
							$upload['type'] : $_SERVER['CONTENT_TYPE'],
					isset($upload['error']) ? $upload['error'] : null,
					null,
					$content_range
				);
			}
			
			return $this->generate_response(
				array($this->options['param_name'] => $files),
				$print_response
			);
		}
	}
?>